package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.Member;
import cn.wolfcode.crm.query.MemberQueryObject;
import cn.wolfcode.crm.service.ILevelService;
import cn.wolfcode.crm.service.IMemberService;
import cn.wolfcode.crm.util.JsonResult;
import cn.wolfcode.crm.util.PageResult;
import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.util.List;

@Controller
@RequestMapping("/member")
public class MemberController {
    @Autowired
    private IMemberService memberService;
    @Autowired
    private ILevelService levelService;

    @RequestMapping("")
    public String index(){
        return "member";
    }

    @RequestMapping("/list")
    @ResponseBody
    public PageResult list(MemberQueryObject qo,String tagIds){
        if(tagIds!=null){
            String[] split = tagIds.split(",");
            Long[] str2 = new Long[split.length];
            for (int i = 0; i < split.length; i++) {
                str2[i] = Long.valueOf(split[i]);
            }
            qo.setTagIds(str2);
        }
        return memberService.list(qo);
    }

    @RequestMapping("/queryByKeyword")
    @ResponseBody
    public Member queryByKeyword(String keyword){
        return memberService.queryByKeyword(keyword);
    }



    @RequestMapping("/listAll")
    @ResponseBody
    public List<Member> listAll(){
        return memberService.listAll();
    }


    @RequestMapping("/save")
    @ResponseBody
    public JsonResult save(Member member){
        try {
            memberService.save(member);
            return new JsonResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonResult("保存失败!");
        }
    }



    @RequestMapping("/lossCard")
    @ResponseBody
    public JsonResult lossCard(Long id){
        try {
            memberService.lossCard(id);
            return new JsonResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonResult("挂失失败!");
        }
    }



    @RequestMapping("/cancelLossCard")
    @ResponseBody
    public JsonResult cancelLossCard(Long id){
        try {
            memberService.cancelLossCard(id);
            return new JsonResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonResult("解挂失败!");
        }
    }



    @RequestMapping("/moveGroup")
    @ResponseBody
    public JsonResult moveGroup(Long id,Long newGroupId){
        try {
            memberService.moveGroup(id,newGroupId);
            return new JsonResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonResult("移动失败!");
        }
    }



    @RequestMapping("/addTag")
    @ResponseBody
    public JsonResult addTag(Long id,Long newTagId){
        try {
            memberService.addTag(id,newTagId);
            return new JsonResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonResult("会员标签设置失败!");
        }
    }



    @RequestMapping("/resetPassword")
    @ResponseBody
    public JsonResult resetPassword(Long id){
        try {
            memberService.resetPassword(id);
            return new JsonResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonResult("密码重置失败!");
        }
    }



    @RequestMapping("/modifyPassword")
    @ResponseBody
    public JsonResult modifyPassword(Long id,String oldPassword,String newPassword){
        try {
            memberService.modifyPassword(id,oldPassword,newPassword);
            return new JsonResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonResult(e.getMessage());
        }
    }

    @RequestMapping("/update")
    @ResponseBody
    public JsonResult update(Member member){
        try {
            memberService.update(member);
            return new JsonResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonResult("更新失败!");
        }
    }


    @RequestMapping("/remove")
    @ResponseBody
    public JsonResult remove(Long id){
        try {
            memberService.remove(id);
            return new JsonResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonResult("删除失败!");
        }
    }
    
    
    
    @RequestMapping("/export")
    @ResponseBody
    public void export(MemberQueryObject qo,HttpServletResponse response) throws Exception {
        //先查询出符合条件的数据
        qo.setRows(10000);//可能分页没有100,所以这里手动设置,以便将所有符合条件的的数据都查出来
        List<Member> members = memberService.queryForExport(qo);
        //设置响应头
        response.setHeader("Content-Disposition","attachment;filename=member.xls");
        OutputStream outputStream = response.getOutputStream();
        //创建一个可写入的工作簿:当关闭这个工作簿时,会自动创建一个电子表格,并流向输入流,通过http从servlet发给浏览器
        WritableWorkbook workbook = Workbook.createWorkbook(outputStream);
        WritableSheet sheet = workbook.createSheet("会员信息",0);
        //创建一行单元格,装列名
        //创建一系列单元格,装列数据
        sheet.addCell(new Label(0,0,"会员id"));
        sheet.addCell(new Label(1,0,"会员卡号"));
        sheet.addCell(new Label(2,0,"会员名字"));
        sheet.addCell(new Label(3,0,"密码"));
        sheet.addCell(new Label(4,0,"qq"));
        sheet.addCell(new Label(5,0,"email"));
        sheet.addCell(new Label(6,0,"生日"));
        sheet.addCell(new Label(7,0,"手机号"));
        sheet.addCell(new Label(8,0,"微信"));
        sheet.addCell(new Label(9,0,"地址"));
        sheet.addCell(new Label(10,0,"兴趣"));
        sheet.addCell(new Label(11,0,"备注"));
        sheet.addCell(new Label(12,0,"提醒"));
        sheet.addCell(new Label(13,0,"会员卡注册日期"));
        sheet.addCell(new Label(14,0,"会员头像"));
        sheet.addCell(new Label(15,0,"积分"));
        sheet.addCell(new Label(16,0,"开卡金额"));
        sheet.addCell(new Label(17,0,"赠送金额"));
        sheet.addCell(new Label(18,0,"账户余额"));
        //=======================以下是对象类型字段==============================
        sheet.addCell(new Label(19,0,"会员等级"));
        sheet.addCell(new Label(20,0,"开卡员工"));
        sheet.addCell(new Label(21,0,"称谓"));
        sheet.addCell(new Label(22,0,"推荐人"));
        sheet.addCell(new Label(23,0,"会员卡状态"));
        sheet.addCell(new Label(24,0,"会员标签"));
        sheet.addCell(new Label(25,0,"注册来源"));
        sheet.addCell(new Label(26,0,"所属门店"));
        sheet.addCell(new Label(27,0,"支付方式"));
        sheet.addCell(new Label(28,0,"分组"));
        for(int i=0;i<members.size();i++){
            int row=i+1;
            sheet.addCell(new Label(0,row, members.get(i).getId().toString()));
            sheet.addCell(new Label(1,row, members.get(i).getCardSn()));
            sheet.addCell(new Label(2,row, members.get(i).getUsername()));
            sheet.addCell(new Label(3,row, members.get(i).getPassword()));
            sheet.addCell(new Label(4,row, members.get(i).getQq()));
            sheet.addCell(new Label(5,row, members.get(i).getEmail()));
            sheet.addCell(new Label(6,row, members.get(i).getBirthday()==null?"":members.get(i).getBirthday().toLocaleString()));
            sheet.addCell(new Label(7,row,members.get(i).getTel()));
            sheet.addCell(new Label(8,row,members.get(i).getWechat()));
            sheet.addCell(new Label(9,row,members.get(i).getAddress()));
            sheet.addCell(new Label(10,row,members.get(i).getInterest()));
            sheet.addCell(new Label(11,row,members.get(i).getRemark()));
            sheet.addCell(new Label(12,row,members.get(i).getTips()));
            sheet.addCell(new Label(13,row,members.get(i).getRegistTime()==null?"":members.get(i).getRegistTime().toLocaleString()));
            sheet.addCell(new Label(14,row,members.get(i).getImagePath()));
            sheet.addCell(new Label(15,row,members.get(i).getAccumulation()==null?"":members.get(i).getAccumulation().toString()));
            sheet.addCell(new Label(16,row,members.get(i).getInitAccount()==null?"":members.get(i).getInitAccount().toString()));
            sheet.addCell(new Label(17,row,members.get(i).getGiftAccount()==null?"":members.get(i).getGiftAccount().toString()));
            sheet.addCell(new Label(18,row,members.get(i).getTotalAccount()==null?"":members.get(i).getTotalAccount().toString()));
            sheet.addCell(new Label(19,row,members.get(i).getLevel()==null?"":members.get(i).getLevel().getName()));
            sheet.addCell(new Label(20,row,members.get(i).getInputUser()==null?"":members.get(i).getInputUser().getName()));
            sheet.addCell(new Label(21,row,members.get(i).getGender()==null?"":members.get(i).getGender().getName()));
            sheet.addCell(new Label(22,row,members.get(i).getRecommender()==null?"":members.get(i).getRecommender().getUsername()));
            sheet.addCell(new Label(23,row,members.get(i).getState()==null?"":members.get(i).getState().getName()));
            sheet.addCell(new Label(24,row,members.get(i).getTag()==null?"":members.get(i).getTag().getName()));
            sheet.addCell(new Label(25,row,members.get(i).getResource()==null?"":members.get(i).getResource().getName()));
            sheet.addCell(new Label(26,row,members.get(i).getShop()==null?"":members.get(i).getShop().getName()));
            sheet.addCell(new Label(27,row,members.get(i).getPayway()==null?"":members.get(i).getPayway().getName()));
            sheet.addCell(new Label(28,row,members.get(i).getMgroup()==null?"":members.get(i).getMgroup().getName()));
        }
        workbook.write();
        workbook.close();
    }
}
